package huangjinkuanggong;

import java.awt.*;

public class Line {
    //起点坐标
    int x = 330;
    int y = 60;

    //终点坐标
    int endx = 500;
    int endy = 500;

    //线长
    double MIN_length = 100;
    double MAX_length = 750;
    double length = 150;
    double n = 0;

    //方向
    int dir = 1;
    //状态 0为摇摆 1为抓取 2为收回 3抓取返回

    int state;
    GameWin frame;
    Line(GameWin frame){
        this.frame= frame;
    }
    public void logic(){
        for(Object object :this.frame.objectList){
            if(endx > object.x && endx < object.x + object.width&&
                    endy > object.y && endy < object.y + object.height){
                state = 3;
                object.flag = true;
            }
        }
    }
    public void lines(Graphics g){
        endx = (int)(x + length*Math.cos(n*Math.PI));
        endy = (int)(y + length*Math.sin(n*Math.PI));
        g.setColor(Color.red);
        g.drawLine(x-1,y,endx-1,endy);
        g.drawLine(x,y,endx,endy);
        g.drawLine(x+1,y,endx+1,endy);
        //g.drawLine(x,y,endx,endy);
    }
    public void paintSelf(Graphics g){
        logic();
        switch (state){
            case 0:
                if(n < 0.1){
                    dir = 1;
                }else if(n > 0.9){
                    dir = -1;
                }
                n= n + 0.005 * dir;
                lines(g);
                break;
            case 1:
                if(length < MAX_length){
                    length = length + 5;
                    lines(g);
                }else {
                    state = 2;
                }
                break;
            case 2:
                if(length > MIN_length){
                    length=length - 5 ;
                    lines(g);
                }else {
                    state = 0;
                }
            case 3:
                int m = 1;
                if(length > MIN_length){
                    length=length-5;
                    lines(g);
                    for(Object object :this.frame.objectList){
                        if(object.flag){
                            m= object.m;
                            object.x = endx -object.getWidth()/2;
                            object.y= endx;
                            if(length <= MIN_length){
                                object.x= 150;
                                object.y= 150;
                                object.flag = false;
                                //加分
                                BackGround.count+= object.count;
                                state = 0;
                            }
                        }
                    }
                }
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            default:
        }
    }
}
